home *** CD-ROM | disk | FTP | other *** search
/ Otherware / Otherware_1_SB_Development.iso / mac / developm / source / rkeyboar.cpt / Reactive Keyboard ƒ / Utils.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-07-30  |  5.1 KB  |  243 lines

  1. #include <MacIncludes.h>
  2.  
  3. extern    Boolean     gHaveAUX;
  4.  
  5. #define hide            1
  6. #define show            0
  7.  
  8. struct ClipStack {
  9.     RgnHandle Clip;
  10.     struct ClipStack *Next;
  11. } *TheStacks[2];
  12.  
  13.  
  14. void PushClip(int stack)
  15. {
  16.     struct ClipStack *node;
  17.  
  18.     node = (struct ClipStack *)NewPtr(sizeof(struct ClipStack));
  19.     node -> Clip = NewRgn();
  20.     GetClip(node->Clip);
  21.     node -> Next = TheStacks[stack];
  22.     TheStacks[stack] = node;
  23. }
  24.  
  25. void PopClip(int stack)
  26. {
  27.     struct ClipStack *node;
  28.     
  29.     if(TheStacks[stack]){
  30.         SetClip(TheStacks[stack]->Clip);
  31.         DisposeRgn(TheStacks[stack]->Clip);
  32.         node = TheStacks[stack]->Next;
  33.         DisposPtr((Ptr)TheStacks[stack]);
  34.         TheStacks[stack] = node;
  35.     } else {
  36.         char debug[100];
  37.         sprintf(debug,"Stack underflow in PopClip, called with stack %d",stack);
  38.         debugstr(debug);
  39.     }
  40. }
  41.  
  42. void EnableDItem(DialogPtr dialog, short item, short state)
  43. {
  44.     short        kind;
  45.     Handle        h;
  46.     Rect        r;
  47.  
  48.     GetDItem(dialog,item,&kind,&h,&r);
  49.     if(state==hide)
  50.         HideControl((ControlHandle)h);
  51.     else
  52.         ShowControl((ControlHandle)h);
  53.         
  54. }
  55.  
  56. char *pStrcat(s,t)
  57.     unsigned char *s, *t;
  58. {
  59.     unsigned char *s2;
  60.     short tLen;
  61.  
  62.     s2 = s + *s;
  63.     *s += (tLen = *t);
  64.     for (++tLen; --tLen; s2[tLen] = t[tLen]);
  65.     return (s);
  66. }
  67.  
  68. char *pStrcpy(s,t)
  69.     unsigned char *s, *t;
  70. {
  71.     short    tLen;
  72.  
  73.     for (tLen = *t + 1; tLen--; s[tLen] = t[tLen]);
  74.     return (s);
  75. }
  76.  
  77. char * PathNameFromDirID(long DirID, short vRefNum, char *s)
  78. {
  79.     CInfoPBRec    block;
  80.     Str255        directoryName;
  81.     int         err;
  82.  
  83.  
  84.     *s = 0;
  85.     
  86.     block.dirInfo.ioNamePtr = &directoryName;
  87.     block.dirInfo.ioDrParID = DirID;
  88.  
  89.     do {
  90.         block.dirInfo.ioVRefNum = vRefNum;
  91.         block.dirInfo.ioFDirIndex = -1;
  92.         block.dirInfo.ioDrDirID = block.dirInfo.ioDrParID;
  93.  
  94.         err = PBGetCatInfo(&block,false);
  95.         if(err){
  96.             s[0]=0;
  97.             return((char *)-1);
  98.         }
  99.         if (gHaveAUX) {
  100.             if (directoryName[1] != '/')
  101.                 /* If this isn't root (i.e. '/'), append a slash ('/') */
  102.                 pStrcat(&directoryName,"\p/");
  103.         } else 
  104.             /* Append a Macintosh style colon (':') */
  105.             pStrcat(&directoryName,"\p:");
  106.         pStrcat(&directoryName,s);
  107.         pStrcpy(s,&directoryName);
  108.     } while (block.dirInfo.ioDrDirID != 2);
  109.  
  110.     return(s);
  111. }
  112.  
  113. char * PathNameFromWD(long    vRefNum,    char    *s)    
  114. {
  115.  
  116.     WDPBRec    myBlock;
  117.     int err;
  118.  
  119.     /*
  120.     /* PBGetWDInfo has a bug under A/UX 1.1.  If vRefNum is a real vRefNum
  121.     /* and not a wdRefNum, then it returns garbage.  Since A/UX has only 1
  122.     /* volume (in the Macintosh sense) and only 1 root directory, this can
  123.     /* occur only when a file has been selected in the root directory (/).
  124.     /* So we look for this and hardcode the DirID and vRefNum. */
  125.  
  126.     if (gHaveAUX && (vRefNum == -1))
  127.         return(PathNameFromDirID(2,-1,s));
  128.  
  129.     myBlock.ioNamePtr = nil;
  130.     myBlock.ioVRefNum = vRefNum;
  131.     myBlock.ioWDIndex = 0;
  132.     myBlock.ioWDProcID = 0;
  133.  
  134.     /* Change the Working Directory number in vRefnum into a real vRefnum */
  135.     /* and DirID. The real vRefnum is returned in ioVRefnum, and the real */
  136.     /* DirID is returned in ioWDDirID. */
  137.  
  138.     err=PBGetWDInfo(&myBlock,false);
  139.     if(err){
  140.         s[0]=0;
  141.         return((char *)-1);
  142.     }
  143.  
  144.     return(PathNameFromDirID(myBlock.ioWDDirID,myBlock.ioWDVRefNum,s));
  145. };
  146.  
  147. long GetDirID(long vRefNum)
  148. {
  149.     WDPBRec    myBlock;
  150.     int err;
  151.  
  152.     /*
  153.     /* PBGetWDInfo has a bug under A/UX 1.1.  If vRefNum is a real vRefNum
  154.     /* and not a wdRefNum, then it returns garbage.  Since A/UX has only 1
  155.     /* volume (in the Macintosh sense) and only 1 root directory, this can
  156.     /* occur only when a file has been selected in the root directory (/).
  157.     /* So we look for this and hardcode the DirID and vRefNum. */
  158.  
  159.     if (gHaveAUX && (vRefNum == -1))
  160.         return(2);
  161.  
  162.     myBlock.ioNamePtr = nil;
  163.     myBlock.ioVRefNum = vRefNum;
  164.     myBlock.ioWDIndex = 0;
  165.     myBlock.ioWDProcID = 0;
  166.  
  167.     /* Change the Working Directory number in vRefnum into a real vRefnum */
  168.     /* and DirID. The real vRefnum is returned in ioVRefnum, and the real */
  169.     /* DirID is returned in ioWDDirID. */
  170.  
  171.     err=PBGetWDInfo(&myBlock,false);
  172.     if(err){
  173.         return((long)-1);
  174.     }
  175.  
  176.     return(myBlock.ioWDDirID);
  177. }
  178.  
  179. short GetRealRefNum(long vRefNum)
  180. {
  181.     WDPBRec    myBlock;
  182.     int err;
  183.     char debug[100];
  184.  
  185.     /*
  186.     /* PBGetWDInfo has a bug under A/UX 1.1.  If vRefNum is a real vRefNum
  187.     /* and not a wdRefNum, then it returns garbage.  Since A/UX has only 1
  188.     /* volume (in the Macintosh sense) and only 1 root directory, this can
  189.     /* occur only when a file has been selected in the root directory (/).
  190.     /* So we look for this and hardcode the DirID and vRefNum. */
  191.  
  192.     if (gHaveAUX && (vRefNum == -1))
  193.         return(-1);
  194.  
  195.     myBlock.ioNamePtr = nil;
  196.     myBlock.ioVRefNum = vRefNum;
  197.     myBlock.ioWDIndex = 0;
  198.     myBlock.ioWDProcID = 0;
  199.  
  200.     /* Change the Working Directory number in vRefnum into a real vRefnum */
  201.     /* and DirID. The real vRefnum is returned in ioVRefnum, and the real */
  202.     /* DirID is returned in ioWDDirID. */
  203.  
  204.     err=PBGetWDInfo(&myBlock,false);
  205.     if(err){
  206.         sprintf(debug,"PBGetWDInf error: %d",err);
  207.         debugstr(debug);
  208.     }
  209.  
  210.     return(myBlock.ioWDVRefNum);
  211. }
  212.  
  213.  
  214. int Log2(int v)
  215. {
  216.     int x;
  217.     
  218.     if(v<=0)
  219.         return(0);
  220.     
  221.     for(x=0;(v-1)>>x;x++);
  222.     return(x);
  223. }
  224.  
  225.  
  226. #define TopLeft(aRect)    (* (Point *) &(aRect).top)
  227. #define BotRight(aRect)    (* (Point *) &(aRect).bottom)
  228.  
  229.  
  230. void GetGlobalRect (WindowPtr window, Rect *globalRect)
  231. {
  232.     GrafPtr savePort;
  233.  
  234.     GetPort(&savePort);
  235.     SetPort(window);
  236.     *globalRect = window->portRect;
  237.     LocalToGlobal(&TopLeft(*globalRect));
  238.     LocalToGlobal(&BotRight(*globalRect));
  239.     SetPort(savePort);
  240. }
  241.  
  242.  
  243.